package cn.lili.modules.aop.interceptor;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.modules.member.entity.dos.RedPackUser;
import cn.lili.modules.member.entity.dos.RedPackUserHistory;
import cn.lili.modules.member.service.RedPackUserHistoryService;
import cn.lili.modules.member.service.RedPackUserService;
import cn.lili.modules.payment.entity.dto.UserRedPackUpdateDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * 红包操作切面
 *
 * @author Chopper
 * @since 2020/11/17 7:22 下午
 */
@Slf4j
@Aspect
@Component
@RequiredArgsConstructor
public class RedPackLogInterceptor {

    private final RedPackUserService redPackUserService;

    private final RedPackUserHistoryService redPackUserHistoryService;

    @After("@annotation(cn.lili.modules.aop.annotation.RedPackLogPoint)")
    public void doAfter(JoinPoint pjp) {
        log.info("进入红包日志");
        //参数
        Object[] obj = pjp.getArgs();
        try {
            //变动红包
            if (obj[0] != null) {
                UserRedPackUpdateDTO userRedPackUpdateDTO = (UserRedPackUpdateDTO) obj[0];
                Integer redPack = userRedPackUpdateDTO.getRedPack();
                //变动类型
                Boolean isAdd = true;
                if (userRedPackUpdateDTO.getUserRedPackServiceEnum() != null) {
                    isAdd = userRedPackUpdateDTO.getUserRedPackServiceEnum().getAdd();
                }
                // 会员ID
                String memberId = "";
                if (CharSequenceUtil.isNotEmpty(userRedPackUpdateDTO.getUserId())) {
                    memberId = userRedPackUpdateDTO.getUserId();
                }
                // 变动积分为0，则直接返回
                if (redPack == 0) {
                    return;
                }

                //根据会员id查询会员信息
                RedPackUser redPackUser = redPackUserService.getRedPackUser(memberId);
                RedPackUserHistory userRedPackHistory = new RedPackUserHistory();
                userRedPackHistory.setMemberId(redPackUser.getUserId());
                userRedPackHistory.setMemberName(redPackUser.getUserName());
                userRedPackHistory.setRedPackType(userRedPackUpdateDTO.getUserRedPackServiceEnum().name());

                userRedPackHistory.setVariableRedPack(redPack);
                if (Boolean.TRUE.equals(isAdd)) {
                    userRedPackHistory.setBeforeRedPack(redPackUser.getRedPackCount() - redPack);
                } else {
                    userRedPackHistory.setBeforeRedPack(redPackUser.getRedPackCount() + redPack);
                }
                userRedPackHistory.setRedPack(redPackUser.getRedPackCount());
                userRedPackHistory.setContent(userRedPackUpdateDTO.getDescription());
                userRedPackHistory.setCreateBy("系统");
                redPackUserHistoryService.save(userRedPackHistory);
            }
        } catch (Exception e) {
            log.error("红包操作错误", e);
        }
    }

}
